/*
 * seth-main.c -- Network device example over Raw Sockets 
 *
 * Copyright (C) 2011 Saeedm <saeed.tx@gmail.com>
 *
 * The source code in this file can be freely used, adapted,
 * and redistributed in source or binary form, so long as an
 * acknowledgment appears in derived source files.  
 * No warranty is attached;
 * we cannot take responsibility for errors or fitness for use.
 *
 * $Id: seth-main.c,v 1.21 20011/10/1 02:36:03 Saeedm Exp $
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>

#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/errno.h>  /* error codes */
#include <linux/types.h>  /* size_t */
#include <linux/interrupt.h> /* mark_bh */

#include <linux/proc_fs.h>	/* Necessary because we use proc fs */
#include <asm/uaccess.h>	/* for copy_*_user */

#include "gmod.h"

//operations include
#include "op_sample.h"


MODULE_AUTHOR("SaeedM - saeed.tx@gmail.com");
MODULE_DESCRIPTION("Generic Module");
MODULE_LICENSE("Dual BSD/GPL");


/**
 * This structure hold information about the /proc file
 *
 */
static struct proc_dir_entry *proc_root;


/**
 * The buffer used to store character for this module
 *
 */
static char procfs_buffer[PROCFS_MAX_SIZE];

/**
 * The size of the buffer
 *
 */
static unsigned long procfs_buffer_size = 0;

/** 
 * This function is called then the /proc file is read
 *
 */
int 
procfile_read(char *buffer,char **buffer_location,off_t offset, int buffer_length, int *eof, void *data)
{
	int ret;
	struct gmod_op* op = (struct gmod_op*)data;	
	printk(KERN_INFO "procfile_read (/proc/%s/%s) called for\n", PROCFS_NAME,op->name);
	
	if (offset > 0) {
		/* we have finished to read, return 0 */
 		ret  = 0;
	} else {
		/* fill the buffer, return the buffer size */
		memcpy(buffer, procfs_buffer, procfs_buffer_size);
		ret = procfs_buffer_size;
	}
	//TODO : PASS DATA
	op->op_run(NULL,op->data);
	return ret;
}

/**
 * This function is called with the /proc file is written
 *
 */
int procfile_write(struct file *file, const char *buffer, unsigned long count,void *data)
{
	struct gmod_op* op = (struct gmod_op*)data;
	printk(KERN_INFO "procfile_write (/proc/%s/%s) \n", PROCFS_NAME,op->name);	
	/* get buffer size */
	procfs_buffer_size = count;
	if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
		procfs_buffer_size = PROCFS_MAX_SIZE;
	}
	
	/* write data to the buffer */
	if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
		return -EFAULT;
	}
	
	op->op_parse(procfs_buffer,procfs_buffer_size,op->data);
	return procfs_buffer_size;
}

static int create_procfss(void){
	struct gmod_op* op;
	struct proc_dir_entry *new_proc_file;

	for (op = op_head; op != NULL ; op=op->next){
		printk(KERN_INFO "creating /proc/%s/%s : %s \n",PROCFS_NAME,op->name, op->descr);
		new_proc_file = create_proc_entry(op->name, 0777, proc_root);
		if (!new_proc_file) 
		{
			printk(KERN_ALERT "Error: Could not initialize /proc/%s/%s\n",PROCFS_NAME,op->name);			
			return -1;
		}
		new_proc_file->data = op;
		new_proc_file->read_proc  = procfile_read;
		new_proc_file->write_proc = procfile_write;
		new_proc_file->mode       = S_IFREG | S_IRUGO;			
	}
	return 0;
}

static void destroy_procfss(void){
	struct gmod_op* op;
	for (op = op_head; op != NULL ; op=op->next){
		printk(KERN_INFO "destroying /proc/%s/%s : %s \n", PROCFS_NAME,op->name, op->descr);
		remove_proc_entry(op->name, proc_root);
	}
}

static int __init gmod_init_module(void)
{
	int ret = -ENOMEM;
	ret = 0;
	/* create the /proc file */
	
	register_operation(&info_op);
	register_operation(&sample_op);
	proc_root = proc_mkdir(PROCFS_NAME,NULL);
	
	if (proc_root == NULL) {
			printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",PROCFS_NAME);
			return -ENOMEM;
	}
	//proc_file->owner = THIS_MODULE;
		
	//proc_file->read_proc  = procfile_read;
	//proc_file->write_proc = procfile_write;
	//proc_file->mode       = S_IFREG | S_IRUGO;
	//proc_file->uid        = 0;
	//proc_file->gid        = 0;
	//proc_file->size       = 37;

	printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME);
	if (create_procfss())
	{
		destroy_procfss();
		remove_proc_entry(PROCFS_NAME, NULL);
		printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
		return -ENOMEM;
	}	
	return ret;
}

static void __exit gmod_cleanup(void)
{
	destroy_procfss();
	remove_proc_entry(PROCFS_NAME, NULL);
	printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
}

module_init(gmod_init_module);
module_exit(gmod_cleanup);
